package com.cz.ssyx.acl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.ssyx.acl.mapper.AdminMapper;
import com.cz.ssyx.acl.service.AdminRoleService;
import com.cz.ssyx.acl.service.AdminService;
import com.cz.ssyx.acl.service.RoleService;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.common.utils.MD5;
import com.cz.ssyx.model.acl.Admin;
import com.cz.ssyx.model.acl.AdminRole;
import com.cz.ssyx.vo.acl.AdminQueryVo;
import com.cz.ssyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/4/15 19:40
 * @version: 1.0
 */

@Api(tags = "用户管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/acl/user")
public class AdminController {

    @Autowired
    private AdminService adminService;
    
    @Autowired
    private RoleService roleService;

    //1.用户列表
    @ApiOperation("用户列表")
    @GetMapping("/{current}/{limit}")
    public Result listPage(@PathVariable Long current, @PathVariable Long limit, AdminQueryVo adminQueryVo){

        String username = adminQueryVo.getUsername();

        Page<Admin> page = new Page<>(current, limit);

        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(username), Admin::getUsername, username);

        Page<Admin> adminPage = adminService.page(page, queryWrapper);
        return Result.ok(adminPage);
    }

    //2.id查用户
    @ApiOperation("根据id查用户")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    //3.新增用户
    @ApiOperation("新增用户")
    @PostMapping("/save")
    public Result save(@RequestBody Admin admin){
        //用户名不能重复
        String username = admin.getUsername();
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(username), Admin::getUsername, username);
        Admin is_admin = adminService.getOne(queryWrapper);

        if (is_admin == null){
            String password = admin.getPassword();
            admin.setPassword(MD5.encrypt(password));
            adminService.save(admin);
            return Result.ok(null);
        }else {
            return Result.fail("用户名重复");
        }
    }

    //4.修改用户
    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin){
        adminService.updateById(admin);
        return Result.ok(null);
    }

    //5.删除用户
    @ApiOperation("删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        adminService.removeById(id);
        return Result.ok(null);
    }

    //6.批量删除用户
    @ApiOperation("批量删除用户")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        adminService.removeByIds(ids);
        return Result.ok(null);
    }

    //7.获取用户的所有角色
    @ApiOperation("获取用户的所有角色")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId){
        //返回所有角色和该用户的所有角色
        Map<String, Object> map = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }

    //8.为用户分配角色
    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId, @RequestParam List<Long> roleId){

        roleService.saveAdminRole(adminId, roleId);

        return Result.ok(null);
    }
}
